<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Db;
class Mycontroller extends Controller
{
     public function __construct()
    {
        Parent::__construct();
        //查询用户信息
        $user = db('user')->where('id','=',Session::get('userid'))->find();
        $this->assign('user',$user);
        // var_dump($user);die;
        //查询部门所有用户
        $userlist = db('user')->where('class','=',$user['class'])->select();
        $this->assign('userlist',$userlist);
        //发送本部门名称
        $classname = db('class')->where('id','=',$user['class'])->value('name');
        $this->assign('classname',$classname);

        //查询系统消息发送前台
            // $re = Db::table('read_sysnews s')
            //     ->join('read_combosys c','s.id = c.sid','left')
            //     ->field('s.id sid,s.newscontent,s.sysstarttime,s.sysendtime,c.id cid,c.sid,c.uid,c.status')
            //     ->where('sysstarttime','<',time())->where('sysendtime','>',time())
            //     ->select();
            // $this->assign('sysnews',$re);
        //将系统表 和关联表联查 找到该部门的消息 且时间满足的发送前台
            // $intersys = Db::table('sys_sys s')
            //             ->join('sys_revsys r','s.id=r.sid','left')
            //             ->field('s.*,r.uid,r.sid,r.status')
            //             ->where('starttime','<',time())->where('endtime','>',time())->where('class','=',$user['class'])->where('status','<',2)
            //             ->select();

            $intersys = db('sys')->where('starttime','<',time())->where('endtime','>',time())->where('class','=',$user['class'])->select();

            //遍历去除已经阅读过的
            $revsys = db('revsys')->where('uid','=',$user['id'])->column('sid');
            foreach($intersys as $k=>$v){
                //判断是否阅读过该消息
                if(in_array($v['id'], $revsys)){
                    unset($intersys[$k]);
                }
            }
            // var_dump($intersys,$revsys);die();
            if($intersys){
                $this->assign('intersys',$intersys);
            }else{
                $this->assign('intersys','');
            }
            // var_dump($user['id'],$user['class']);


            //查询单条消息
            $news = db('news')->where('getid','=',$user['id'])->where('status','<',2)->select();
            // var_dump($news);die;
            if($news){
                $this->assign('news',$news);
            }else{
                $this->assign('news','');
            }
    }
   
}
